package com.example.lenovo.weiyi;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.com.ruanmeng.demon.JianCeXQM;
import com.com.ruanmeng.utils.ImgDataUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.com.ruanmeng.view.CircularImage;
import com.com.ruanmeng.view.MyListView;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import music.Player;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;
import yulanpic.ImagePagerActivity;

import static music.Player.needxh;

/**
 * 检测详情
 */
public class JianCeXQActivity extends BaseActivity implements MediaPlayer.OnCompletionListener {

    @BindView(R.id.imv_jcxq_userpic)
    CircularImage imvJcxqUserpic;
    @BindView(R.id.tv_years)
    TextView tvYears;
    @BindView(R.id.tv_sg)
    TextView tvSg;
    @BindView(R.id.tv_weight)
    TextView tvWeight;
    @BindView(R.id.mlist_jcxq_pf)
    MyListView mlistJcxqPf;
    @BindView(R.id.mlist_jcxq_zdaction)
    MyListView mlistJcxqZdaction;
    @BindView(R.id.imv_jcxq_sex)
    ImageView imvJcxqSex;
    @BindView(R.id.tv_jcxq_name)
    TextView tvJcxqName;
    ArrayList<JianCeXQM.UserEvalItemsBean> Temp_pflist = new ArrayList<>();
    ArrayList<JianCeXQM.EvalScoresBean> Temp_actionlist = new ArrayList<>();
    @BindView(R.id.music_action_progressa)
    SeekBar musicActionProgressa;
    private PingFenAdapter pfadapter;
    private ZhiDaoAdapter zdadapter;
    private Player player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jian_ce_xq);
        ButterKnife.bind(this);
        ChangLayTitle("检测报告");
        LayBack();
        init();
        getData();
    }

    public void init() {
        ImgDataUtil.loadYuanImage(JianCeXQActivity.this, HttpIP.Base_IpIMage + PreferencesUtils.getString(JianCeXQActivity.this, "url"), imvJcxqUserpic);
//        ImgDataUtil.loadYuanImage(JianCeXQActivity.this, HttpIP.Base_IpIMage + getIntent().getStringExtra("uname"), imvJcxqUserpic);
        tvJcxqName.setText(getIntent().getStringExtra("uname"));
        tvYears.setText(getIntent().getStringExtra("age"));
        tvSg.setText(getIntent().getStringExtra("sg") + "cm");
        tvWeight.setText(getIntent().getStringExtra("weight") + "kg");
        //1是男
        if (("1").equals(getIntent().getStringExtra("sex"))) {
            imvJcxqSex.setBackgroundResource(R.drawable.gender_boy);
        } else {
            imvJcxqSex.setBackgroundResource(R.drawable.gender_girl);
        }
        //评分结果
        pfadapter = new PingFenAdapter(Temp_pflist);
        mlistJcxqPf.setAdapter(pfadapter);
        mlistJcxqPf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            }
        });
        //指导动作
        zdadapter = new ZhiDaoAdapter(Temp_actionlist);
        mlistJcxqZdaction.setAdapter(zdadapter);
        /*mlistJcxqZdaction.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                YuLanPic(HttpIP.Base_IpIMage + Temp_actionlist.get(position).getGuidePic());
            }
        });*/
        //关于音乐
        player = new Player(musicActionProgressa);
        needxh = 0;//不需要循环播放
        player.mediaPlayer.setOnCompletionListener(this);
    }

    public void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.JCReportsXQ, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(JianCeXQActivity.this, "uid"));
        mRequest.add("assessId", getIntent().getStringExtra("aid"));
        // 添加到请求队列
        CallServer.getRequestInstance().add(JianCeXQActivity.this, 0, mRequest,
                new CustomHttpListener(JianCeXQActivity.this, true, JianCeXQM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        JianCeXQM model = ((JianCeXQM) data);
                        if(model.getEvalScores()!=null){
                            Temp_actionlist.addAll(model.getEvalScores());
                        }
                        if(model.getUserEvalItems()!=null) {
                            Temp_pflist.addAll(model.getUserEvalItems());
                        }
                        zdadapter.notifyDataSetChanged();
                        pfadapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                    }
                }, true, true);
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        for (int i = 0; i < Temp_actionlist.size(); i++) {
            Temp_actionlist.get(i).setCheck(0);
        }
        zdadapter.notifyDataSetChanged();
    }

    /**
     * 评分结果
     */
    class PingFenAdapter extends BaseAdapter {
        private List<JianCeXQM.UserEvalItemsBean> list;

        public PingFenAdapter(List<JianCeXQM.UserEvalItemsBean> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View v, ViewGroup parent) {
            v = getLayoutInflater().inflate(R.layout.item_pf, null);
            TextView name = (TextView) v.findViewById(R.id.tv_pf_name);
            TextView tv_pf_standerd = (TextView) v.findViewById(R.id.tv_pf_standerd);
            TextView points = (TextView) v.findViewById(R.id.tv_pf_pointsl);
            TextView tv_pf_pointsr = (TextView) v.findViewById(R.id.tv_pf_pointsr);
            LinearLayout li_pf_see = (LinearLayout) v.findViewById(R.id.li_pf_see);
            View view_ban = (View) v.findViewById(R.id.view_pfxq_ban);
            LinearLayout view_man = (LinearLayout) v.findViewById(R.id.view_pfxq_man);
            name.setText(list.get(position).getEvalItemName());
            tv_pf_standerd.setText(list.get(position).getStandard());
            //只有左的话    只显示结果     右  如果没有的话     就显示一个  有的话显示  左 结果    右结果
            //result3  表示左      result4  表示右
            if(!TextUtils.isEmpty(list.get(position).getResult4())){
                points.setText("左"+list.get(position).getResult3());
                tv_pf_pointsr.setText("右"+list.get(position).getResult4());
            }else {
                points.setText(list.get(position).getResult3());
            }
//            points.setText(list.get(position).getScore() + "分");
            if (position == Temp_pflist.size() - 1) {
                view_ban.setVisibility(View.GONE);
                view_man.setVisibility(View.VISIBLE);
            } else {
                view_ban.setVisibility(View.VISIBLE);
                view_man.setVisibility(View.GONE);
            }
            li_pf_see.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent in = new Intent(JianCeXQActivity.this, ChaKanActivity.class);
                    startActivity(in);
                }
            });
            return v;
        }

    }

    /**
     * 指导动作
     */
    class ZhiDaoAdapter extends BaseAdapter {
        private List<JianCeXQM.EvalScoresBean> list;

        public ZhiDaoAdapter(List<JianCeXQM.EvalScoresBean> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View v, ViewGroup parent) {
            v = getLayoutInflater().inflate(R.layout.item_zhidao, null);
            ImageView imv_action_pic = (ImageView) v.findViewById(R.id.imv_zdyj);
            TextView tv_action_content = (TextView) v.findViewById(R.id.tv_action_content);
            View view_ban = (View) v.findViewById(R.id.view_action_ban);
            View view_man = (View) v.findViewById(R.id.view_action_man);
            //语音
            LinearLayout li_action_play = (LinearLayout) v.findViewById(R.id.li_action_play);
            ImageView imv_action_play = (ImageView) v.findViewById(R.id.imv_action_play);
            ImgDataUtil.loadYuanImage(JianCeXQActivity.this, HttpIP.Base_IpIMage + list.get(position).getGuidePic(), imv_action_pic);
            tv_action_content.setMovementMethod(LinkMovementMethod.getInstance());
            tv_action_content.setText(Html.fromHtml(list.get(position).getEvalText()).toString().trim());
            if (!TextUtils.isEmpty(list.get(position).getGuideMusic())) {
                li_action_play.setVisibility(View.VISIBLE);
            }
            if (list.get(position).getCheck() == 1) {
                imv_action_play.setImageResource(R.drawable.zd_music_play_b);
            } else {
                imv_action_play.setImageResource(R.drawable.zd_music_b);
            }
            if (position == Temp_actionlist.size() - 1) {
                view_ban.setVisibility(View.GONE);
                view_man.setVisibility(View.VISIBLE);
            } else {
                view_ban.setVisibility(View.VISIBLE);
                view_man.setVisibility(View.GONE);
            }
            li_action_play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Temp_actionlist.get(position).getCheck() == 1) {
                        for (int i = 0; i < Temp_actionlist.size(); i++) {
                            Temp_actionlist.get(i).setCheck(0);
                        }
                        if (player != null && player.mediaPlayer.isPlaying()) {
                            player.pause();
                        }
                    } else {
                        for (int i = 0; i < Temp_actionlist.size(); i++) {
                            Temp_actionlist.get(i).setCheck(0);
                        }
                        Temp_actionlist.get(position).setCheck(1);
                        playvideo(Temp_actionlist.get(position).getGuideMusic());
                    }
                    zdadapter.notifyDataSetChanged();
                }
            });
            imv_action_pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    YuLanPic(HttpIP.Base_IpIMage + Temp_actionlist.get(position).getGuidePic());
                }
            });
            return v;
        }

    }

    /**
     * 预览图片
     */
    public void YuLanPic(String url) {
        String[] aStrings = new String[1];
        aStrings[0] = url;
        Intent intent1 = new Intent(JianCeXQActivity.this,
                ImagePagerActivity.class); //
        // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
        intent1.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, aStrings);
        intent1.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, 0);
        startActivity(intent1);
//        Intent intent1 = new Intent(JianCeXQActivity.this, MultiImageAct.class);
//        intent1.putExtra("picList",aStrings);
//        intent1.putExtra("position",0);
//        startActivity(intent1);
    }

    /**
     * 关于音乐
     */
    public void playvideo(final String link) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                player.playUrl(HttpIP.Base_IpIMage + link);
            }
        }).start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.stop();
            player = null;
        }
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        if (player == null) {
            player = new Player(musicActionProgressa);
        }
    }
}
